package com.alipay.app.groupmeal.standard.http;

import static com.alipay.app.groupmeal.standard.constant.BaseConstant.DEFAULT_TIMEOUT;

import com.alipay.app.groupmeal.standard.AppContext;
import com.alipay.app.groupmeal.standard.constant.ConfigConstant;
import com.alipay.app.groupmeal.standard.http.conver.FastJsonConverterFactory;
import com.alipay.app.groupmeal.standard.http.cookie.CookieJarImpl;
import com.alipay.app.groupmeal.standard.http.cookie.store.PersistentCookieStore;
import com.alipay.app.groupmeal.standard.http.interceptor.HttpHeaderInterceptor;
import com.alipay.app.groupmeal.standard.http.interceptor.LoggingInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;

public class SysCenterApiEngine {

     private static Retrofit sysApi;
    private volatile static SysCenterApiEngine apiEngine;

    public static SysCenterApiEngine getInstance() {
        if (apiEngine == null) {
            synchronized (SysCenterApiEngine.class) {
                if (apiEngine == null) {
                    apiEngine = new SysCenterApiEngine();
                }
            }
        }
        return apiEngine;
    }

    private SysCenterApiEngine() {
        initRetrofitApi();
    }

    /**
     * 代扣
     */
    private void initRetrofitApi() {
        OkHttpClient client = new OkHttpClient.Builder()
                .cookieJar(new CookieJarImpl(new PersistentCookieStore(AppContext.getContext())))
                .addInterceptor(new HttpHeaderInterceptor())
                .addInterceptor(new LoggingInterceptor())
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();

        sysApi = new Retrofit.Builder()
                .baseUrl(ConfigConstant.SYS_API_URL)
                .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .client(client)
                .build();
    }



    public SysCenterApiService getSysApiService() {
        return sysApi.create(SysCenterApiService.class);
    }

}


